package com.lagou.ipersistence.reflection.invoker;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author Yan Jiahong
 * Created on 2020/11/17
 */
public class MethodInvoker implements Invoker {
    /**
     * 处理的类型
     * 如果是getter方法, 则是返回类型
     * 如果是setter方法, 则是第一个参数的类型
     */
    private final Class<?> type;
    
    private final Method method;
    
    public MethodInvoker(Method method) {
        this.method = method;
        if (method.getParameterTypes().length == 1) {
            // setter方法
            this.type = method.getParameterTypes()[0];
        } else {
            // getter方法
            this.type = method.getReturnType();
        }
    }
    
    @Override
    public Object invoke(Object target, Object[] args) throws IllegalAccessException, InvocationTargetException {
        return method.invoke(target, args);
    }
    
    @Override
    public Class<?> getType() {
        return type;
    }
}
